using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;

namespace ebookreader
{
    public class LogoScreen : DrawableGameComponent
    {
        SpriteBatch sprite_batch;

        Texture2D logo;
        Rectangle texture_rectangle;
        Matrix screen_transform;
        string logo_path;
        float alpha;
        //AStateManager StateManager;
        Game game;
        #region Constructors
        GraphicsDeviceManager graphics;
        public LogoScreen(Game game, string logo_path, GraphicsDeviceManager graphics)
            : base(game)
        {
            alpha = 0.0f;
            this.logo_path = logo_path;
            this.game = game;
            this.graphics = graphics;
        }

        #endregion

        public override void Initialize()
        {
            sprite_batch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            //StateManager = (AStateManager)game.Services.GetService(typeof(AStateManager));

            //graphics.SetPortraitOrientation();
            screen_transform = Game.ScreenTransformMatrix(graphics);
            base.Initialize();
        }

        bool loadingFinished = false;
        protected override void LoadContent()
        {
            logo = Game.Content.Load<Texture2D>(logo_path);
            texture_rectangle = new Rectangle(Game.GraphicsDevice.Viewport.Width / 2 - logo.Width / 2,
                Game.GraphicsDevice.Viewport.Height / 2 - logo.Height / 2,
                logo.Width, logo.Height);


            new Thread(() =>
            {
                Game.Content.Load<Texture2D>("Definitivo/background");
                Game.Content.Load<Texture2D>("Definitivo/bookmark_popup_body");
                Game.Content.Load<Texture2D>("Definitivo/page_popup_body");
                Game.Content.Load<Texture2D>("Definitivo/page_popup_back");
                Game.Content.Load<Texture2D>("Definitivo/popup_read_body");
                Game.Content.Load<Texture2D>("Definitivo/trial");
                Game.Content.Load<Texture2D>("Definitivo/title_box");
                Game.Content.Load<Texture2D>("Definitivo/button_ring");
                Game.Content.Load<Texture2D>("Definitivo/popup_body");
                Game.Content.Load<Texture2D>("Definitivo/bookmark_popup_back");
                //Game.Content.Load<Texture2D>("Definitivo/message_popup");
                Game.Content.Load<Texture2D>("Definitivo/menu");
                Game.Content.Load<Texture2D>("Definitivo/main_book");
                Game.Content.Load<Texture2D>("todo/logo1");
                Game.Content.Load<Texture2D>("todo/logo2");
                
                loadingFinished = true;
            }).Start();

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            alpha += dt/2;

            if (alpha > 1.0f && loadingFinished)
            {
                //graphics.SetLandscapeLeftOrientation();
                ScreenManager.SelectMainMenu(Game);
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            sprite_batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, screen_transform);

            sprite_batch.Draw(logo, texture_rectangle, Color.White * alpha);

            sprite_batch.End();

            base.Draw(gameTime);
        }
    }
}
